package com.yyfax.pay.config.data.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.framework.exception.YYSqlException;
import com.yyfax.framework.sql.SQLFactory;
import com.yyfax.pay.config.data.dao.PaymentQuotaDao;
import com.yyfax.pay.config.model.po.PaymentQuotaPO;

@Repository
public class PaymentQuotaDaoImpl extends BaseDaoImpl<PaymentQuotaPO, Integer> implements PaymentQuotaDao {

	// 这里写死,将来可以考虑注入
	private static final String TABLE_NAME = "payment_quota";

	private static final String SQL_INSERT = "insert";

	private static final String SQL_UPDATE = "update";

	private static final String SQL_SELECT_FOR_LIST = "selectForList";
	/** 根据渠道ID查询 */
	private static final String SQL_SELECT_BY_ID = "selectById";
	/** 根据商户号和支付号查询限额 **/
	private static final String SQL_SELECT_BY_MERANDPAY = "selectByMerAndPay";

	private static final String SQL_SELECT_BY_MERCH_PAYMENT_BANK = "selectByMerchAndPaymentCodeAndBankCode";

	private static final String SQL_SELECT_BY_MERCH_PAYMENT_BANK_SUBJECT_PAYMODE = "selectByMerchAndPaymentCodeAndBankCodeAndSubjectAndPayMode";

	@Autowired
	private SQLFactory sqlFactory;

	public PaymentQuotaDaoImpl() {
		super(TABLE_NAME);
	}

	@Override
	public int insert(PaymentQuotaPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);

		return namedParameterJdbcTemplate.insertForPK(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public PaymentQuotaPO selectById(int id) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id", id);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PaymentQuotaPO>(PaymentQuotaPO.class));
	}

	@Override
	public List<PaymentQuotaPO> selectForList() throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_FOR_LIST);

		return namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<PaymentQuotaPO>(PaymentQuotaPO.class));
	}

	@Override
	public int update(PaymentQuotaPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);

		return namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public List<PaymentQuotaPO> selectByMerAndPay(String merchantCode, String paymentCode) throws Exception {
		MapSqlParameterSource params = new MapSqlParameterSource();

		params.addValue("merchantCode", merchantCode);
		params.addValue("paymentCode", paymentCode);
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERANDPAY);

		return namedParameterJdbcTemplate.query(sql.toString(), params, new BeanPropertyRowMapper<PaymentQuotaPO>(PaymentQuotaPO.class));
	}

	@Override
	public PaymentQuotaPO selectByMerchAndPaymentCodeAndBankCode(String merchantCode, String paymentCode, String bankCode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCH_PAYMENT_BANK);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("paymentCode", paymentCode);
		params.addValue("bankCode", bankCode);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PaymentQuotaPO>(PaymentQuotaPO.class));
	}

	@Override
	public PaymentQuotaPO selectByMerchAndPaymentCodeAndBankCodeAndSubjectTypeAndPayMode(String merchantCode, String paymentCode, String bankCode,
			String subjectType, String payMode) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_MERCH_PAYMENT_BANK_SUBJECT_PAYMODE);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("paymentCode", paymentCode);
		params.addValue("bankCode", bankCode);
		params.addValue("subjectType", subjectType);
		params.addValue("payMode", payMode);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<PaymentQuotaPO>(PaymentQuotaPO.class));
	}

}
